#include <iostream>
#include<fcntl.h>
#include<unistd.h>
#define maxsize 20
using namespace std;
class KVDBHandler {
 public:
 int32_t fd;
 KVDBHandler(const std::string& db_file){
     const char* path = db_file.c_str();
      fd = open(path, O_RDWR|O_CREAT|O_APPEND , S_IRUSR|S_IWUSR);
     if(fd == -1){
          cout << "path wrong" << endl;
     }
 };
 ~KVDBHandler(){close(fd);};
 int32_t set(const std::string& key, const std::string& value)
 {
     int32_t num_key[1];
     num_key[0] = key.length();
     write(fd, num_key , 4);
     int32_t num_value[1];
     num_value[0] = value.length();
     write(fd, num_value , 4);
     const char *key_val = key.c_str();
     const char *value_val = value.c_str();
     write(fd , key_val , key.length());
     write(fd , value_val , value.length());
        return true;
     }

// int get(const std::string& key , std::string& value) const{
 char * get(const std::string& key) const{
    char* sub_key = new char[32];
    char* sub_value = new char[256];
    int eof = lseek(fd,0,SEEK_END);
    lseek(fd,0,SEEK_SET);
    int32_t len_key,len_value;
    while(lseek(fd,0,SEEK_CUR)!=eof){
        read(fd,&len_key,sizeof(len_key));
        read(fd,&len_value,sizeof(len_value));
        read(fd,sub_key,len_key);
        if((std::string)sub_key == key){
            if(len_value!=-1){
                read(fd,sub_value,len_value);
                //std::cout << "read sub_value succeeded\n";
            }
            else{
                read(fd,sub_value,sizeof(int32_t));
                //std::cout << "well, this is del command\n";
            }
        }
        else{
            if(len_value!=-1)
            lseek(fd,len_value,SEEK_CUR);
            else lseek(fd,sizeof(int32_t),SEEK_CUR);
            //std::cout << "NOT THIS!!!\n";
        }
    }
    return sub_value;
    delete sub_key;
    delete sub_value;
}
 int32_t del(const std::string& key){
     int num_key[1];
     num_key[0] = key.length();
     write(fd, num_key , 4);
     int num_value[1];
     num_value[0] = -1;
     write(fd, num_value , 4);
     const char *key_val = key.c_str();
     write(fd , key_val , key.length());
     const char* value_val = new char[256];
     write(fd , value_val , sizeof(int32_t));
     delete value_val;
     return true;
     }
int purge(KVDBHandler &purgetest){
    int32_t fd_val = purgetest.fd;
         if(fd_val == -1) {
           cout << "open wrong";
           return 0;
         }
         else {
            int32_t flag = 1;
            string *key = new string [100];
            int32_t num_key = 0;
            char* sub_key = new char[32];
            char* sub_value = new char[256];
            int eof = lseek(fd,0,SEEK_END);
            lseek(fd,0,SEEK_SET);
            int32_t len_key,len_value;
            while(lseek(fd,0,SEEK_CUR)!=eof){
              read(fd,&len_key,sizeof(len_key));
              read(fd,&len_value,sizeof(len_value));
              read(fd,sub_key,len_key);
              int32_t flag = 1;
        for(int32_t i = 0; i < num_key; i++)
        if(key[i] == string(sub_key)){flag = 0; break;}
        if(flag == 1) key[num_key++] = (string)sub_key;
            if(len_value!=-1)
            lseek(fd,len_value,SEEK_CUR);
            else lseek(fd,sizeof(int32_t),SEEK_CUR);
    }
    delete sub_key;
    delete sub_value;
        for(int32_t i = 0; i < num_key; i++)
       {
           if(get(key[i])[0]){
            flag = purgetest.set(key[i] , get(key[i]));
            if(!flag){break;}
           }
       }
             if(flag){
                 return 1;
             }
             else {
                 cout << "no space" << endl;
                 return 0;
             }
         }

}

// int purge(int32_t fd_val){
//     if(fd_val == -1){
//         cout << "fulled!" << endl;
//         return 0;
//     }
//     else{
//         string *key = new string [100];
//     int32_t num_key = 0;
//     char* sub_key = new char[32];
//     char* sub_value = new char[256];
//     int eof = lseek(fd,0,SEEK_END);
//     lseek(fd,0,SEEK_SET);
//     int32_t len_key,len_value;
//     while(lseek(fd,0,SEEK_CUR)!=eof){
//         read(fd,&len_key,sizeof(len_key));
//         read(fd,&len_value,sizeof(len_value));
//         read(fd,sub_key,len_key);
//         int32_t flag = 1;
//         for(int32_t i = 0; i < num_key; i++)
//         if(key[i] == string(sub_key)){flag = 0; break;}
//         if(flag == 1) key[num_key++] = (string)sub_key;
//             if(len_value!=-1)
//             lseek(fd,len_value,SEEK_CUR);
//             else lseek(fd,sizeof(int32_t),SEEK_CUR);
//     }
//     delete sub_key;
//     delete sub_value;
//         for(int32_t i = 0; i < num_key; i++)
//        {
//            if(get(key[i])[0]){
//                  int32_t num_key[1];
//      num_key[0] = key[i].length();
//      write(fd, num_key , 4);
//      int32_t num_value[1];
//      num_value[0] = sizeof(get(key[i]));
//      write(fd, num_value , 4);
//      write(fd , key[i].c_str() , key[i].length());
//      write(fd , get(key[i]) , sizeof(get(key[i])));
//            }
//        }
//     //    fd = fd_val;
//         return 1;
//     }
// }
 };


int main(){
    KVDBHandler test("1111"),afterpurge("kv");
    int t;
    cin >> t;
    string key_val , value_val , order;
    while(t--){
      cin >> order >> key_val;
      if(key_val.length() == 0){cout << "wrong key" << endl;continue;}
      if(order == "set"){
          cin >> value_val;
          if(value_val.length() == 0){cout << "wrong value" << endl;continue;}
          test.set(key_val , value_val);
      }
      else if(order == "get"){
        //   char *value_val = new char [256];
        //   test.get(key_val, value_val);
          char *value_val = test.get(key_val);
          if(value_val[0]) cout << value_val << endl;
          else cout << "get false" << endl;
      }
      else{
          test.del(key_val);}
    }
	//从文件头偏移到文件尾，lseek返回的偏移量就是文件大小
	int32_t res = lseek(test.fd, 0, SEEK_END);
	if(res > maxsize) {
        int32_t flag = test.purge(afterpurge);
        if(flag){
            test.fd = afterpurge.fd;
            remove("1111");
            //KVDBHandler tag("1111");
            //afterpurge.fd = tag.fd;
        }
        }
    }
